<template>
  <div class="supplier-form">
    <div class="form-header">
      <h3>{{ isEdit ? '编辑供应商' : '新增供应商' }}</h3>
      <button @click="closeForm" class="close-btn">×</button>
    </div>

    <form @submit.prevent="submitForm" class="form-content">
      <div class="form-row">
        <div class="form-group">
          <label>供应商编号 *</label>
          <input 
            v-model="formData.supplierCode" 
            type="text" 
            required 
            placeholder="请输入供应商编号"
          />
        </div>
        <div class="form-group">
          <label>供应商名称 *</label>
          <input 
            v-model="formData.supplierName" 
            type="text" 
            required 
            placeholder="请输入供应商名称"
          />
        </div>
      </div>

      <div class="form-row">
        <div class="form-group">
          <label>联系人 *</label>
          <input 
            v-model="formData.contactPerson" 
            type="text" 
            required 
            placeholder="请输入联系人姓名"
          />
        </div>
        <div class="form-group">
          <label>联系电话 *</label>
          <input 
            v-model="formData.contactPhone" 
            type="tel" 
            required 
            placeholder="请输入联系电话"
          />
        </div>
      </div>

      <div class="form-row">
        <div class="form-group">
          <label>身份证号</label>
          <input 
            v-model="formData.contactIdNumber" 
            type="text" 
            placeholder="请输入身份证号"
          />
        </div>
        <div class="form-group">
          <label>供应商类型</label>
          <select v-model="formData.supplierType">
            <option value="企业">企业</option>
            <option value="个人">个人</option>
            <option value="政府">政府</option>
          </select>
        </div>
      </div>

      <div class="form-row">
        <div class="form-group full-width">
          <label>地址</label>
          <div class="address-input-group">
            <input 
              v-model="formData.address" 
              type="text" 
              placeholder="请选择或输入地址"
              readonly
            />
            <button 
              type="button" 
              @click="showMapSelector" 
              class="map-btn"
            >
              地图定位
            </button>
          </div>
          <div v-if="formData.longitude && formData.latitude" class="coordinate-info">
            <small>坐标: {{ formData.longitude.toFixed(6) }}, {{ formData.latitude.toFixed(6) }}</small>
          </div>
        </div>
      </div>

      <div class="form-row">
        <div class="form-group">
          <label>所属行业</label>
          <input 
            v-model="formData.industry" 
            type="text" 
            placeholder="请输入所属行业"
          />
        </div>
        <div class="form-group">
          <label>统一社会信用代码</label>
          <input 
            v-model="formData.creditCode" 
            type="text" 
            placeholder="请输入统一社会信用代码"
          />
        </div>
      </div>

      <div class="form-row">
        <div class="form-group">
          <label>助记码</label>
          <input 
            v-model="formData.mnemonicCode" 
            type="text" 
            placeholder="请输入助记码"
          />
        </div>
        <div class="form-group">
          <label>服务人员</label>
          <input 
            v-model="formData.servicePersonnel" 
            type="text" 
            placeholder="请输入服务人员"
          />
        </div>
      </div>

      <div class="form-row">
        <div class="form-group">
          <label>业务员</label>
          <input 
            v-model="formData.salesperson" 
            type="text" 
            placeholder="请输入业务员"
          />
        </div>
        <div class="form-group">
          <label>供应商属性</label>
          <input 
            v-model="formData.supplierAttributes" 
            type="text" 
            placeholder="请输入供应商属性"
          />
        </div>
      </div>

      <div class="form-row">
        <div class="form-group">
          <label>归属单位</label>
          <input 
            v-model="formData.belongsToUnit" 
            type="text" 
            placeholder="请输入归属单位"
          />
        </div>
        <div class="form-group">
          <label>所属市场</label>
          <input 
            v-model="formData.market" 
            type="text" 
            placeholder="请输入所属市场"
          />
        </div>
      </div>

      <div class="form-row">
        <div class="form-group full-width">
          <label>备注</label>
          <textarea 
            v-model="formData.memo" 
            placeholder="请输入备注信息"
            rows="3"
          ></textarea>
        </div>
      </div>

      <div class="form-row">
        <div class="form-group">
          <label class="checkbox-label">
            <input 
              v-model="formData.isActive" 
              type="checkbox" 
            />
            启用状态
          </label>
        </div>
      </div>

      <div class="form-actions">
        <button type="submit" class="submit-btn">
          {{ isEdit ? '保存修改' : '新增供应商' }}
        </button>
        <button type="button" @click="closeForm" class="cancel-btn">
          取消
        </button>
      </div>
    </form>

    <!-- 地图地址选择器 -->
    <MapAddressSelector
      v-model="formData.address"
      :show-modal="showMapModal"
      @update:show-modal="showMapModal = $event"
      @confirm="handleAddressConfirm"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed, watch } from 'vue'
import MapAddressSelector from './MapAddressSelector.vue'
import { api } from '../utis/request'

// 定义组件属性
interface Props {
  supplier?: any
  visible: boolean
}

// 定义组件事件
interface Emits {
  (e: 'update:visible', value: boolean): void
  (e: 'success'): void
}

const props = withDefaults(defineProps<Props>(), {
  supplier: null,
  visible: false
})

const emit = defineEmits<Emits>()

// 响应式数据
const showMapModal = ref(false)
const isEdit = computed(() => !!props.supplier)

// 表单数据
const formData = reactive({
  supplierCode: '',
  supplierName: '',
  contactPerson: '',
  contactPhone: '',
  contactIdNumber: '',
  address: '',
  latitude: 0, // 纬度
  longitude: 0, // 经度
  industry: '',
  creditCode: '',
  memo: '',
  supplierType: '企业',
  mnemonicCode: '',
  servicePersonnel: '',
  salesperson: '',
  supplierAttributes: '',
  belongsToUnit: '',
  market: '',
  isActive: true
})

// 监听供应商数据变化
watch(() => props.supplier, (newSupplier) => {
  if (newSupplier) {
    Object.assign(formData, newSupplier)
  } else {
    // 重置表单
    Object.assign(formData, {
      supplierCode: '',
      supplierName: '',
      contactPerson: '',
      contactPhone: '',
      contactIdNumber: '',
      address: '',
      latitude: 0,
      longitude: 0,
      industry: '',
      creditCode: '',
      memo: '',
      supplierType: '企业',
      mnemonicCode: '',
      servicePersonnel: '',
      salesperson: '',
      supplierAttributes: '',
      belongsToUnit: '',
      market: '',
      isActive: true
    })
  }
}, { immediate: true })

// 显示地图选择器
const showMapSelector = () => {
  showMapModal.value = true
}

// 处理地址确认
const handleAddressConfirm = (address: string, location: { lng: number, lat: number }) => {
  formData.address = address
  formData.longitude = location.lng
  formData.latitude = location.lat
  console.log('选择的地址:', address)
  console.log('坐标:', location)
}

// 提交表单
const submitForm = async () => {
  try {
    if (isEdit.value) {
      await api.supplier.update(props.supplier.id, formData)
    } else {
      await api.supplier.create(formData)
    }
    
    emit('success')
    closeForm()
  } catch (error) {
    console.error('提交表单失败:', error)
    alert('操作失败，请重试')
  }
}

// 关闭表单
const closeForm = () => {
  emit('update:visible', false)
}
</script>

<style scoped>
.supplier-form {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.form-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #eee;
}

.form-header h3 {
  margin: 0;
  color: #333;
  font-size: 18px;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #999;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.close-btn:hover {
  color: #333;
}

.form-content {
  background: white;
  border-radius: 8px;
  width: 90%;
  max-width: 800px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.form-row {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
  padding: 0 20px;
}

.form-group {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.form-group.full-width {
  flex: 2;
}

.form-group label {
  font-size: 14px;
  color: #333;
  margin-bottom: 8px;
  font-weight: 500;
}

.form-group input,
.form-group select,
.form-group textarea {
  padding: 10px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  transition: border-color 0.3s;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  outline: none;
  border-color: #1890ff;
}

.form-group textarea {
  resize: vertical;
  min-height: 80px;
}

.address-input-group {
  display: flex;
  gap: 10px;
  align-items: center;
}

.address-input-group input {
  flex: 1;
}

.map-btn {
  background: #1890ff;
  color: white;
  border: none;
  padding: 10px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  white-space: nowrap;
}

.map-btn:hover {
  background: #40a9ff;
}

.coordinate-info {
  margin-top: 5px;
  color: #666;
  font-size: 12px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.checkbox-label input[type="checkbox"] {
  width: auto;
  margin: 0;
}

.form-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  padding: 20px;
  border-top: 1px solid #eee;
}

.submit-btn,
.cancel-btn {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.submit-btn {
  background: #52c41a;
  color: white;
}

.submit-btn:hover {
  background: #73d13d;
}

.cancel-btn {
  background: #f5f5f5;
  color: #666;
  border: 1px solid #ddd;
}

.cancel-btn:hover {
  background: #e6e6e6;
}
</style>
